<template>
	<view>
		<view class="address-choose-box" v-if="JSON.stringify(address)==='{}'">
			<button type="primary" size="mini" class="choose-address" @click="chooseAddress">请选择收货地址 + </button>
		</view>

		<!-- 渲染收货信息的盒子 -->
		<view class="address-info-box" @click="chooseAddress" v-else>
			<view class="address-info-top">
				<view class="left">
					<view class="username">收货人: <text>{{ address.userName }}</text> </view>
				</view>
				<view class="right">
					<view class="phone">电话: <text>{{ address.telNumber }}</text></view>
					<uni-icons type="arrowright" size="16"></uni-icons>
				</view>
			</view>
			<view class="address-info-bottom">
				<view class="left">收货地址: </view>
				<view class="right">{{ addresslast }}</view>
			</view>
		</view>

		<image src="/static/address-border.png" class="address-border"></image>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations,
		mapGetters
	} from 'vuex'

	export default {
		name: "my-address",
		data() {
			return {
				// address: {}
			};
		},
		methods: {
			...mapMutations('m_user', ['updateAddress']),
			async chooseAddress() {
				// 调用uni.chooseAddress() 实现 收货地址 功能
				// const res = await uni.chooseAddress().catch(err => err)
				// console.log(res);
				// 解构赋值
				const [err, succ] = await uni.chooseAddress().catch(err => err)
				console.log(err, succ);
				if (err === null && succ.errMsg === 'chooseAddress:ok') {
					// this.address = succ

					this.updateAddress(succ)
				}
				// 用户未授权
				if (err && (err.errMsg === 'chooseAddress:fail auth deny' || err.errMsg === 'chooseAddress:fail authorize no response')) 
					// 调用 this.reAuth() 方法·向用户重新发起授权申请
					this.reAuth()
				
			},
			async reAuth() {
				// 提示用户对地址进行授权
				const [error, confirmResult] = await uni.showModal({
					content: '检测到您没打开地址权限，是否去设置打开？',
					confirmText: '确认',
					cancelText: '取消',
				})

				// 如果弹框异常 则直接退出
				if (error) return

				// 取消  提示用户
				if (confirmResult.cancle) return uni.$showMsg('您取消了地址授权')
				// 确认 调用uni.openSetting 方法 进行授权页面 重新授权
				if (confirmResult.confirm) return uni.openSetting({
					success: (settingResult) => {
						console.log(settingResult);
						if (settingResult.authSetting['scope.address']) return uni.$showMsg(
							'授权成功！请选择地址')
						if (!settingResult.authSetting['scope.address']) return uni.$showMsg(
							'您取消了地址授权！')
					}
				})
			}
		},
		computed: {
			...mapState('m_user', ['address']),
			...mapGetters('m_user', ['addresslast'])
		}
	}
</script>

<style lang="scss">
	.address-choose-box {
		height: 90px;
		display: flex;
		align-items: center;
		justify-content: center;


	}

	.address-border {
		display: block;
		width: 100%;
		height: 5px;
	}

	.address-info-box {
		height: 90px;
		padding: 0 5px;
		font-size: 12px;
		display: flex;
		flex-direction: column;
		justify-content: center;

		.address-info-top {
			display: flex;
			align-items: center;
			justify-content: space-between;

			.left {
				.username {
					text {
						margin-left: 5px;
					}
				}
			}

			.right {
				display: flex;
				align-items: center;
				justify-content: space-between;

				.phone {
					text {
						margin-left: 5px;
					}
				}
			}
		}

		.address-info-bottom {
			display: flex;
			align-items: center;
			justify-content: start;
			margin-top: 10px;

			.left {
				white-space: nowrap;
				margin-right: 5px;
			}
		}
	}
</style>